/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       material_display.cpp

	$Header: /game/material_display.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "material_display.h"

#include "abbreviate_number.h"
#include "bitmap_group.h"
#include "bitmap_layer_window.h"
#include "interface_help.h"
#include "material_icons.h"
#include "material_names.h"
#include "player.h"
#include "standard_fonts.h"
#include "text_window.h"

// -------------------------------------------------------------------------
// object to display a set of materials
// -------------------------------------------------------------------------
void t_material_display::init( t_bitmap_group_ptr bitmaps, t_player* player, 
							   t_screen_point origin, t_window* parent )
{
	int                   i;
	std::string           number_name;
	t_bitmap_layer const* layer;
	t_help_block const&   help = get_help_block( "material_display" );


	m_player = player;
	for (i = 0; i < k_material_count; i++)
	{
		if( m_windows_active.test( i ) )
		{
			layer = bitmaps->find( k_material_keyword[i] );
			if (layer == 0)
				continue;

			m_window[i] = new t_bitmap_layer_window( layer, -origin, parent );
			set_help( m_window[i], help, k_material_keyword[i] );
			if ( !m_visible )
				m_window[ i ]->set_visible( false );

			number_name = k_material_keyword[i];
			number_name += "_number";

			layer = bitmaps->find( number_name );
			if (layer == 0)
				continue;

			t_screen_rect rect = layer->get_rect() - origin;

			m_material_text[i] = new t_text_window( get_font( rect.height() ), 
													rect, parent, "", 
													t_pixel_24(0,0,0) );
			if ( !m_visible )
				m_material_text[ i ]->set_visible( false );
			m_material_text[i]->set_drop_shadow( true, t_pixel_24(200,200,200));
			set_help( m_material_text[i], help, k_material_keyword[i] );
			m_material_text[i]->set_center_horizontal();
		}
	} 

	update();
}

// -------------------------------------------------------------------------
// object to display a set of materials
// -------------------------------------------------------------------------
void t_material_display::set_material_visible( t_material type, bool arg )
{
	m_windows_active.set( type, arg );
	if ( m_visible )
	{
		if ( m_window[ type ] != 0 )
			m_window[ type ]->set_visible( arg );
		if ( m_material_text[ type ] != 0 )
			m_material_text[ type ]->set_visible( arg );
	}
}

// -------------------------------------------------------------------------
// object to display a set of materials
// -------------------------------------------------------------------------
void t_material_display::set_visible( bool visible )
{
	if ( visible == m_visible )
		return;

	m_visible = visible;

	int i;
	for ( i = 0; i < k_material_count; ++i )
	{
		if ( !m_windows_active[ i ] )
			continue;
		if ( m_window[ i ] != 0 )
			m_window[ i ]->set_visible( m_visible );
		if ( m_material_text[ i ] != 0 )
			m_material_text[ i ]->set_visible( m_visible );
	}
}

// -------------------------------------------------------------------------
// object to display a set of materials
// -------------------------------------------------------------------------
void t_material_display::update()
{
	int                     i;
	t_material_array const& funds = m_player->get_funds();
	t_text_window*			window;

	for (i = 0; i < k_material_count; i++)
	{
		if( m_windows_active.test( i ) )
		{
			window = m_material_text[i];
			if (window == 0)
				continue;

			t_font const&	font = *window->get_font();
			int				width = window->get_width();

			window->set_text( abbreviate_number( funds[i], font, width ));
		}
	}
}
